/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ScheduledTaskCallback;
/*     */ import com.zimbra.common.util.TaskScheduler;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.db.DbPool;
/*     */ import com.zimbra.cs.db.DbPool.DbConnection;
/*     */ import com.zimbra.cs.db.DbScheduledTask;
/*     */ import com.zimbra.cs.mailbox.acl.ExpireGrantsTaskCallback;
/*     */ import com.zimbra.cs.mailbox.alerts.CalItemReminderTaskCallback;
/*     */ import java.util.Date;
/*     */ import java.util.Random;
/*     */ import java.util.concurrent.Callable;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ScheduledTaskManager
/*     */ {
/*     */   private static TaskScheduler<ScheduledTaskResult> sScheduler;
/*  41 */   private static Random sRandom = new Random();
/*     */   
/*     */   public static void startup() throws ServiceException
/*     */   {
/*  45 */     if (sScheduler != null) {
/*  46 */       ZimbraLog.scheduler.info("Scheduled tasks have already been initialized", new Exception());
/*  47 */       return;
/*     */     }
/*     */     
/*     */ 
/*  51 */     Provisioning prov = Provisioning.getInstance();
/*  52 */     int numThreads = prov.getLocalServer().getIntAttr("zimbraScheduledTaskNumThreads", 20);
/*  53 */     int minThreads = numThreads / 2;
/*  54 */     sScheduler = new TaskScheduler(null, minThreads, numThreads);
/*  55 */     sScheduler.addCallback(new TaskCleanup());
/*  56 */     sScheduler.addCallback(new CalItemReminderTaskCallback());
/*  57 */     sScheduler.addCallback(new ExpireGrantsTaskCallback());
/*     */     
/*  59 */     for (ScheduledTask task : DbScheduledTask.getTasks(null, 0)) {
/*     */       try {
/*  61 */         schedule(null, task);
/*     */       } catch (ServiceException e) {
/*  63 */         ZimbraLog.scheduler.warn("Unable to schedule %s.", task, e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void schedule(ScheduledTask task)
/*     */     throws ServiceException
/*     */   {
/*  75 */     DbPool.DbConnection conn = null;
/*     */     try {
/*  77 */       conn = DbPool.getConnection();
/*  78 */       schedule(conn, task);
/*  79 */       conn.commit();
/*     */     } finally {
/*  81 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void schedule(DbPool.DbConnection conn, ScheduledTask task)
/*     */     throws ServiceException
/*     */   {
/*  97 */     if (conn != null) {
/*  98 */       DbScheduledTask.createTask(conn, task);
/*     */     }
/*     */     
/* 101 */     if (task.isRecurring())
/*     */     {
/*     */ 
/* 104 */       long delay = Math.abs(sRandom.nextLong()) % task.getIntervalMillis();
/* 105 */       sScheduler.schedule(getKey(task), task, true, task.getIntervalMillis(), delay);
/*     */     } else {
/* 107 */       if (task.getExecTime() == null) {
/* 108 */         throw ServiceException.FAILURE("Exec time not set for scheduled task.", null);
/*     */       }
/* 110 */       long delay = task.getExecTime().getTime() - System.currentTimeMillis();
/* 111 */       if (delay < 0L) {
/* 112 */         delay = 0L;
/*     */       }
/* 114 */       sScheduler.schedule(getKey(task), task, delay);
/*     */     }
/*     */   }
/*     */   
/*     */   public static ScheduledTask getTask(String className, String taskName, int mailboxId) {
/* 119 */     return (ScheduledTask)sScheduler.getTask(getKey(className, taskName, mailboxId));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static ScheduledTask cancel(String className, String taskName, int mailboxId, boolean mayInterruptIfRunning)
/*     */     throws ServiceException
/*     */   {
/* 127 */     DbPool.DbConnection conn = null;
/* 128 */     ScheduledTask task = null;
/*     */     try {
/* 130 */       conn = DbPool.getConnection();
/* 131 */       task = cancel(conn, className, taskName, mailboxId, mayInterruptIfRunning);
/* 132 */       conn.commit();
/*     */     } finally {
/* 134 */       DbPool.quietClose(conn);
/*     */     }
/* 136 */     return task;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static ScheduledTask cancel(DbPool.DbConnection conn, String className, String taskName, int mailboxId, boolean mayInterruptIfRunning)
/*     */     throws ServiceException
/*     */   {
/* 149 */     if (conn != null) {
/* 150 */       DbScheduledTask.deleteTask(conn, className, taskName);
/*     */     }
/* 152 */     return (ScheduledTask)sScheduler.cancel(getKey(className, taskName, mailboxId), mayInterruptIfRunning);
/*     */   }
/*     */   
/*     */   private static String getKey(String className, String taskName, int mailboxId) {
/* 156 */     StringBuilder sb = new StringBuilder();
/* 157 */     sb.append(className).append(':').append(taskName);
/* 158 */     if (mailboxId > 0) {
/* 159 */       sb.append(':').append(mailboxId);
/*     */     }
/* 161 */     return sb.toString();
/*     */   }
/*     */   
/*     */   private static String getKey(ScheduledTask task) {
/* 165 */     return getKey(task.getClass().getName(), task.getName(), task.getMailboxId());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static class TaskCleanup
/*     */     implements ScheduledTaskCallback<ScheduledTaskResult>
/*     */   {
/*     */     public void afterTaskRun(Callable<ScheduledTaskResult> c, ScheduledTaskResult lastResult)
/*     */     {
/* 177 */       DbPool.DbConnection conn = null;
/* 178 */       ScheduledTask task = (ScheduledTask)c;
/* 179 */       if (task.isRecurring())
/*     */       {
/* 181 */         return;
/*     */       }
/*     */       try
/*     */       {
/* 185 */         conn = DbPool.getConnection();
/* 186 */         DbScheduledTask.deleteTask(conn, task.getClass().getName(), task.getName());
/* 187 */         conn.commit();
/*     */       } catch (ServiceException e) {
/* 189 */         ZimbraLog.scheduler.warn("Unable to clean up %s", task, e);
/*     */       } finally {
/* 191 */         DbPool.quietClose(conn);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/ScheduledTaskManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */